﻿using System;

namespace Grace.Data
{
    /// <summary>
    /// C# extension class for IExtraDataContainer
    /// </summary>
    // ReSharper disable once InconsistentNaming
    public static class IExtraDataContainerExtensions
    {
        /// <summary>
        /// Get a value from extra data or return default. will convert 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="container"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        public static T GetExtraDataOrDefaultValue<T>(this IExtraDataContainer container, object key, T defaultValue = default(T))
        {
            var value = container.GetExtraData(key);

            if (value == null)
            {
                return defaultValue;
            }

            if (value is T tValue)
            {
                return tValue;
            }

            return (T)Convert.ChangeType(value, typeof(T));
        }
    }
}
